package com.zhengdongzulin.zuke.widget

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshKernel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.constant.SpinnerStyle
import com.zhengdongzulin.zuke.R

class GifRefreshHeader(context: Context) : LinearLayout(context), RefreshHeader {
    var mImage: ImageView
    var mState: RefreshState = RefreshState.None

    init {
        val inflate = View.inflate(context, R.layout.refresh_header, this)
        mImage = inflate.findViewById(R.id.gif)
        Glide.with(context).asGif().load(R.drawable.refresh_header_gif).into(mImage)
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        mState = newState
    }

    override fun getView(): View {
        return this
    }

    override fun getSpinnerStyle(): SpinnerStyle = SpinnerStyle.Translate

    override fun setPrimaryColors(vararg colors: Int) {
    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {

    }

    override fun onMoving(
        isDragging: Boolean,
        percent: Float,
        offset: Int,
        height: Int,
        maxDragHeight: Int
    ) {
        if (mState == RefreshState.Refreshing) return
        if (percent < 1) {
            mImage.setScaleX(percent)
            mImage.setScaleY(percent)
        }else{
            mImage.setScaleX(1f)
            mImage.setScaleY(1f)
        }
    }

    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        return 0
    }

    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {

    }

    override fun isSupportHorizontalDrag(): Boolean = false
}